const { login, register, getInfo, confirm, change } = require('../controller/user')
const { SuccessModel, ErrorModel } = require('../model/resModel')
const { getExpires } = require('../utils')
const { set } = require('../db/redis')

const { trim, encrypt } = require('../utils')

module.exports = async (req, res) => {
  
  // 登录
  if (req.method === 'POST' && req.path === '/api/user/login') {
    let {username, password} = req.body
    password = encrypt(password)
    try {
      let result = await login(username)
      if (result.length) {
        if (password === result[0].password) {
          req.session = { username, password, role: result[0].role, nickname: result[0].nickname }
          set(req.sessionId, req.session)
          return new SuccessModel('登录成功')
        } else {
          return new SuccessModel('账号或密码错误')
        }
      }
      return new SuccessModel('账号不存在')
    } catch(e) {
      return new ErrorModel('数据库错误')
    }
  }

  // 注册
  if (req.method === 'POST' && req.path === '/api/user/register') {
    const username = trim(req.body.username)
    const password = encrypt(trim(req.body.password))
    if (!username || !password) return new SuccessModel('请输入用户名或密码')
    try {
      let resultData = await register(username, password)
      if (resultData.hasUser) return new SuccessModel('用户名已被注册')
      if (resultData.result.ok === 1) return new SuccessModel('注册成功')
    } catch (e) {
      return new ErrorModel('数据库错误')
    }
  }

  // 登录检查
  if (req.method === 'GET' && req.path === '/api/user/check') {
    if (req.session.username) {
      return new SuccessModel({username: req.session.username}, '已登录')
    } else {
      return new ErrorModel('尚未登录')
    }
  }

  // 退出
  if (req.method === 'GET' && req.path === '/api/user/logout') {
    try {
      res.setHeader('Set-Cookie', `userId=${Date.now()}_${parseFloat(Math.random() * 10)}; path=/; expires=${getExpires()}; httpOnly`)
      return new SuccessModel('已退出')
    } catch (e) {
      return new ErrorModel('退出失败')
    }
  }

  // 获取用户信息
  if (req.method === 'GET' && req.path === '/api/user/info') {
    if (req.session.username) {
      let userInfo = await getInfo(req.session.username)
      return new SuccessModel(userInfo[0], '加载成功')
    } else {
      return new ErrorModel('暂无权限')
    }
  }

  // 修改密码前校验旧密码
  if (req.method === 'POST' && req.path === '/api/user/confirm') {
    const username = trim(req.body.username)
    const password = encrypt(trim(req.body.password))
    let result = await confirm(username, password)
    if (result.length) return new SuccessModel('success')
    return new ErrorModel('error')
  }

  // 修改用户信息
  if (req.method === 'POST' && req.path === '/api/user/modification') {
    const username = trim(req.body.username)
    const password = encrypt(trim(req.body.password))
    const nickname = trim(req.body.nickname)
    let { result } = await change(username, password, nickname)
    if (result.ok === 1) return new SuccessModel('修改成功')
    return new ErrorModel('修改失败')
  }
}
